from django.contrib import admin

from index.models import Book, Author, UserInfo, PubName, ExtendUserinfo


@admin.register(Book)  # 使用admin.register(Model)来注册
class BookAdmin(admin.ModelAdmin):
    list_display = ['id', 'title', 'price', 'retail_price', 'public']  # , 'pub_name'

    def pub_name(self, obj):  # 显示约束字段pubname
        return u'%s' % obj.pub.pubname  # u会对字符串中的\n等进行转义

    pub_name.admin_order_field = 'pub'  # 字段排序
    pub_name.short_description = '出版社'  # 属性name重命名
    list_display_links = ['title', 'public']  # 链接至更改页面，可进入页面修改定义该列值
    list_filter = ['pub__pubname']  # ForeignKey字段
    list_editable = ['price', 'retail_price']  # 定义编辑框
    search_fields = ['title', 'pub__pubname']  # 搜索框
    raw_id_fields = ['pub']


# 注册Model到admin管理后台
admin.site.register([Author, UserInfo, PubName, ExtendUserinfo])
